﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DEQI_Aftermarket.SqlConn;

namespace DEQI_Aftermarket.Controllers
{
    public class HomeController : Controller
    {
        BaseDao dao = new BaseDao();
        public ActionResult Index()
        {
            //if (Request.Cookies["UserId"] == null)
            //{
            //    return RedirectToAction("LoginOut");
            //}
            //string UserName = dao.GetString("t_o_User", "UserName", "and UserId=" + Request.Cookies["UserId"].Value);
            //ViewBag.UserName = UserName;


            return View();
        }
        public ActionResult Login()
        {
            return View("Login");
        }

        public ActionResult LoginCheck()
        {
            string UserName = Request.Form["UserName111"] == null ? null : Request.Form["UserName111"].ToString();
            string PassWord = Request.Form["PassWord"] == null ? null : Request.Form["PassWord"].ToString();


            //进行数据库操作
            string sql = "select count(*) as num from t_user where user_name='" + UserName + "' and user_pwd='" + PassWord + "'";
            List<Dictionary<string, object>> list = new List<Dictionary<string, object>>();
            list = dao.GetList(sql, new string[] { "num" });

            int FindNum = int.Parse(list[0]["num"].ToString());
            if (FindNum > 0)
            {
                return View("Index");

            }
            else
            {
                ViewBag.Msg = "账号或密码错误";
                return View("Login");


            }

        }


        public ActionResult TjxxView()
        {


            string sql = "SELECT count(*) as UserNum FROM t_user where IsDelete=0";
            List<Dictionary<string, object>> list = new List<Dictionary<string, object>>();
            try
            {
                list = dao.GetList(sql, new string[] { "UserNum" });
                string StrNum = list[0]["UserNum"].ToString();
                ViewBag.UserNum = StrNum;

                //ViewBag.UserNum = dao.GetList(sql, new string[] { "UserNum" })[0]["UserNum"].ToString();
            }
            catch (Exception ex)
            {
                Dictionary<string, object> datalog = new Dictionary<string, object>();
                datalog.Add("type", int.Parse("1"));
                datalog.Add("OpTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                datalog.Add("OpID", int.Parse("1"));
                datalog.Add("OpLog", "异常：加载需求管理表格异常 /Home/TjxxView:" + ex.Message.ToString().Replace("'", ""));
                dao.save("T_LOG", datalog);
                return Json('0');
            }
            sql = "SELECT count(*) as MaintainNum FROM t_maintain where IsDelete=0";
            try
            {
                list = dao.GetList(sql, new string[] { "MaintainNum" });
                string StrNum = list[0]["MaintainNum"].ToString();
                ViewBag.MaintainNum = StrNum;

                //ViewBag.UserNum = dao.GetList(sql, new string[] { "UserNum" })[0]["UserNum"].ToString();
            }
            catch (Exception ex)
            {
                Dictionary<string, object> datalog = new Dictionary<string, object>();
                datalog.Add("type", int.Parse("1"));
                datalog.Add("OpTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                datalog.Add("OpID", int.Parse("1"));
                datalog.Add("OpLog", "异常：加载需求管理表格异常 /Home/TjxxView:" + ex.Message.ToString().Replace("'", ""));
                dao.save("T_LOG", datalog);
                return Json('0');
            }



            return View("Tjxx");
        }

        public ActionResult UserView()
        {
            return View("User");
        }

        public ActionResult DJGLView()
        {
            return View("Djgl");
        }

        public ActionResult SelfView()
        {
            return View("Self");
        }

        public ActionResult DjAddView()
        {
            return View("DjAdd");
        }
    }
}